package com.wlr.littleprogram.spider.pipeline;

import com.wlr.littleprogram.dao.CpuInfoDao;
import com.wlr.littleprogram.entity.CpuInfo;
import com.wlr.littleprogram.service.SpiderCpuInfoService;
import com.wlr.littleprogram.spider.exception.SpiderException;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Container;
import org.apache.catalina.Valve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/7/23 19:43
 */
@Component
@Slf4j
public class ZolCpuPipeline implements Pipeline {
    private static SpiderCpuInfoService spiderCpuInfoService;
    private static CpuInfoDao cpuInfoDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void process(ResultItems resultItems, Task task) {
        List<CpuInfo> result = new ArrayList<>();
        try {
            String[] brands = this.getResult(resultItems, "brand");
            String[] titles = this.getResult(resultItems, "title");
            String[] title1s = this.getResult(resultItems, "title1");
            String[] prices = this.getResult(resultItems, "price");
            int time = 0;
            StringBuilder sb = new StringBuilder();
            List<String> newPrices = new ArrayList<>();
            for (String ele : prices) {
                if (("￥").equals(ele.trim()) || ele.trim().matches("^[0-9]*$")) {
                    sb.append(ele);
                    time++;
                } else {
                    newPrices.add(ele);
                }
                if (time == 2) {
                    newPrices.add(sb.toString());
                    sb.setLength(0);
                    time = 0;
                }
            }
            for (int i = 0; i < brands.length; i++) {
                CpuInfo cpuInfo = new CpuInfo();
                String[] s = brands[i].split(" ");
                if ("Intel".equalsIgnoreCase(s[1])) {
                    cpuInfo.setBrandId(1);
                    cpuInfo.setModel(brands[i].substring(6, brands[i].length() - 1));
                } else {
                    cpuInfo.setBrandId(2);
                    cpuInfo.setModel(brands[i].substring(4, brands[i].length() - 1));
                }
                cpuInfo.setTitle(title1s[i] + " " + titles[i].substring(7, titles[i].length() - 7));
                if (("￥").equals(newPrices.get(i).trim()) || newPrices.get(i).trim().matches("//d+")) {
                    cpuInfo.setPrice(newPrices.get(i));
                } else {
                    cpuInfo.setPrice(newPrices.get(i).substring(3));
                }
                result.add(cpuInfo);
            }
        } catch (Exception e) {
            log.error("数据抓取处理错误，{}", e.getMessage());
            e.printStackTrace();
            throw new SpiderException("数据抓取处理错误");
        }
        try {
            for (CpuInfo cpuInfo : result) {
                cpuInfoDao.insert(cpuInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SpiderException("数据持久化异常");
        }
    }

    @Autowired
    public void setSpiderCpuInfoService(SpiderCpuInfoService spiderCpuInfoService) {
        ZolCpuPipeline.spiderCpuInfoService = spiderCpuInfoService;
    }

    @Autowired
    public void setCpuInfoDao(CpuInfoDao cpuInfoDao) {
        ZolCpuPipeline.cpuInfoDao = cpuInfoDao;
    }

    public String[] getResult(ResultItems resultItems, String value) {
        String o = resultItems.get(value);
        String p = " " + o.substring(1, o.length() - 1);
        return p.split(",");
    }

}
